class Registor{
    constructor(){
        this.tell = document.querySelector('.tell');
        this.ipo = document.querySelector('.ipo');
        this.email = document.querySelector('.email');
        this.re_email = document.querySelector('.re-email');
        this.psd = document.querySelector('.psd');
        this.re_psd = document.querySelector('.re-email');
        this.sure = document.querySelector('.sure');
        this.pas = document.querySelector('.pas');
        this.reg = document.querySelector('.reg');
        this.arr = [false,false,false,false];
        this.addEvent();
    }
    addEvent(){
        let that = this;
        // 验证手机号
        this.tell.oninput = function(){
            let re =  /0?(13|14|15|17|18|19)[0-9]{9}/;
            let tell = this.value;
            if(re.test(tell)){
                that.ipo.style.display = 'none';
                that.arr[0] = true;
            }else{
                that.ipo.style.display = 'block';
            }
        }
        // 验证邮箱
        this.email.oninput = function(){
            let re = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
            let email = this.value;
            if(re.test(email)){
                that.re_email.style.display = 'none';
                that.arr[1] = true;
            }else{
                that.re_email.style.display = 'block';
            }
        }
        // 验证密码
        this.psd.oninput = function(){
            let re = /^\w{6,25}$/;
            let psd = this.value;
            if(re.test(psd)){
                that.re_psd.style.display = 'none';
                that.arr[2] = true;
            }else{
                that.re_psd.style.display = 'block';
                that.arr[2] = false;
            }
        }
        // 验证确认密码
        this.sure.oninput = function(){
            let psd = that.psd.value;
            let sure = this.value;
            if(psd === sure){
                that.pas.style.display = 'none';
                that.arr[3] = true;
            }else{
                that.pas.style.display = 'block';
                that.arr[3] = false;
            }
        }
        // 注册按钮
        this.reg.onclick = function(){
            if(that.arr.includes(false)){
                alert('请完善注册信息！');
            }else{
                let cookie_str = $.getCookie('users') ? $.getCookie('users') : '';
                let cookie_obj = $.converStrToObj(cookie_str);
                let tell = that.tell.value;
                let psd = that.psd.value;
                if(tell in cookie_obj){
                    alert('该手机号已注册！');
                    return;
                }else{
                    cookie_obj[tell] = psd;
                    $.cookie('users',JSON.stringify(cookie_obj),{expires : 7,path : '/'});
                    // alert('注册成功！');
                    location.href = '../Login.html';
                }
            }
        }
    }
}
new Registor();